#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<vector>

using namespace std;
using PII = pair<int, int>;

const int N = 110, INF = 0x3f3f3f3f;

int n, m;
int g[N][N];
int level[N];
int state[N];
int dist[N];

int dijkstra(int down, int up){
    memset(dist, 0x3f, sizeof dist);
    memset(state, 0, sizeof state);
    priority_queue<PII, vector<PII>, greater<PII>> que;
    dist[0] = 0;
    que.push({0, 0});
    while(que.size()){
        PII temp = que.top();
        que.pop();

        int ver = temp.second, dis = temp.first;
        if(state[ver]) continue;

        state[ver] = true;

        for (int i = 1; i <= n; i ++){
            if(level[i] >= down && level[i] <= up && dist[i] > dist[ver] + g[ver][i]){
                dist[i] = dist[ver] + g[ver][i];
                que.push({dist[i], i});
            }
        }
    }

    return dist[1];
}

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    memset(g, 0x3f, sizeof g);

    cin >> m >> n;
    for (int i = 1; i <= n; i ++){
        g[i][i] = 0;
    }

    for (int i = 1; i <= n; i++)
    {
        int price, cnt;
        cin >> price >> level[i] >> cnt;
        g[0][i] = min(price, g[0][i]);

        for (int j = 0; j < cnt; j++)
        {
            int p, x;
            cin >> p >> x;
            g[p][i] = min(g[p][i], x);
        }
    }

    int res = INF;
    for (int i = level[1] - m; i <= level[1]; i++){
        res = min(res, dijkstra(i, i + m));
    }

    cout << res << endl;

    return 0;
}